package jp.co.nes.awssupport.common.utility;

import java.util.List;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.elasticsearch.AWSElasticsearch;
import com.amazonaws.services.elasticsearch.AWSElasticsearchClientBuilder;
import com.amazonaws.services.elasticsearch.model.DescribeReservedElasticsearchInstancesRequest;
import com.amazonaws.services.elasticsearch.model.DescribeReservedElasticsearchInstancesResult;
import com.amazonaws.services.elasticsearch.model.ReservedElasticsearchInstance;
import com.amazonaws.services.elasticsearch.model.ResourceNotFoundException;

import jp.co.nes.awssupport.common.base.CommonException;

/**
 * Elasticsearchヘルパーです。<br>
 * Elasticsearchに関する機能を提供します。
 */
public class AwsElasticsearchHelper extends AwsHelperBase {

    /** クライアント */
    private AWSElasticsearch client;

    /**
     * デフォルトコンストラクタです。
     */
    public AwsElasticsearchHelper() {
    }

    /**
     * リージョンを引数にもつコンストラクタです。
     *
     * @param regions リージョン
     * @param accessKey アクセスキー
     * @param secretKey シークレットキー
     */
    public AwsElasticsearchHelper(Regions regions, String accessKey, String secretKey) {
        super(regions, accessKey, secretKey);
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.utility.AwsHelperBase#init(com.amazonaws.regions.Regions, com.amazonaws.auth.AWSCredentials, com.amazonaws.ClientConfiguration)
     */
    @Override
    protected void init(Regions regions, AWSCredentials credentials, ClientConfiguration config) {
        boolean isLocalTest = Boolean.valueOf(PropertiesUtil.getProperty("LocalTest", "false"));
        if (!isLocalTest) {
            if (credentials == null) {
                if (config == null) {
                    client = AWSElasticsearchClientBuilder.standard()
                            .withRegion(regions)
                            .build();
                } else {
                    client = AWSElasticsearchClientBuilder.standard()
                            .withRegion(regions)
                            .withClientConfiguration(config)
                            .build();
                }
            } else {
                if (config == null) {
                    client = AWSElasticsearchClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(new ClientConfiguration())
                            .build();
                } else {
                    client = AWSElasticsearchClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(config)
                            .build();
                }
            }
        }
    }

    /**
     * ReservedElasticsearchInstanceを返却します。<br>
     * reservedElasticsearchInstanceIdに対応するReservedElasticsearchInstanceがなかった場合はnullを返却します。
     *
     * @param reservedElasticsearchInstanceId reservedElasticsearchInstanceId
     * @return ReservedElasticsearchInstance
     */
    public ReservedElasticsearchInstance getReservedElasticsearchInstance(String reservedElasticsearchInstanceId) {
        DescribeReservedElasticsearchInstancesRequest request = new DescribeReservedElasticsearchInstancesRequest();
        request.setReservedElasticsearchInstanceId(reservedElasticsearchInstanceId);
        DescribeReservedElasticsearchInstancesResult result = null;
        try {
            result = client.describeReservedElasticsearchInstances(request);
        } catch (ResourceNotFoundException e) {
            // 存在しなかった場合、何もせず、処理を続行する。
        }

        ReservedElasticsearchInstance reservedElasticsearchInstance = null;
        if (result != null && result.getReservedElasticsearchInstances() != null) {
            List<ReservedElasticsearchInstance> reservedElasticsearchInstanceList = result
                    .getReservedElasticsearchInstances();
            int size = reservedElasticsearchInstanceList.size();
            if (size == 1) {
                reservedElasticsearchInstance = reservedElasticsearchInstanceList.get(0);
            } else if (size > 1) {
                throw new CommonException("size > 1(size=" + size + ")");
            }
        }
        return reservedElasticsearchInstance;
    }
}
